package com.offcn.sellergoods.controller;

import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.sellergoods.entity.GoodsEntity;
import com.offcn.sellergoods.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "商品管理")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/")
    public Result add(@RequestBody GoodsEntity goodsEntity){
        try {
            goodsService.add(goodsEntity);
            return new Result(true, StatusCode.OK,"添加商品成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加商品失败");
        }
    }

    @ApiOperation(value = "根据商品ID查询商品")
    @ApiImplicitParam(name = "goodsId",value = "商品的ID值")
    @GetMapping("/{goodsId}")
    public Result<GoodsEntity> findById(@PathVariable Long goodsId){
        try {
            GoodsEntity goodsEntity = goodsService.findById(goodsId);
            return new Result<>(true,StatusCode.OK,goodsEntity,"根据Id查询商品成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,null,e.getMessage());
        }
    }

    @PutMapping("/audit/{goodsId}")
    public Result audit(@PathVariable Long goodsId){
        try {
            goodsService.audit(goodsId);
            return new Result(true,StatusCode.OK,"商品审核通过");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,e.getMessage());
        }
    }

    /**
     * 商品下架
     */
    @PutMapping("/pull/{goodsId}")
    public Result pull(@PathVariable Long goodsId){
        try {
            goodsService.pull(goodsId);
            return new Result(true,StatusCode.OK,"商品下架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,e.getMessage());
        }
    }

    /**
     * 商品上架
     */
    @PutMapping("/put/{goodsId}")
    public Result put(@PathVariable Long goodsId){
        try {
            goodsService.put(goodsId);
            return new Result(true,StatusCode.OK,"商品上架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,e.getMessage());
        }
    }

    /**
     * 商品批量上架
     */
    @PutMapping("/put/many")
    public Result putMany(@RequestBody Long[] ids){
        goodsService.putMany(ids);
        return new Result(true,StatusCode.OK,"商品批量上架成功");
    }

    @DeleteMapping("/{goodsId}")
    public Result delete(@PathVariable Long goodsId){
        goodsService.delete(goodsId);
        return new Result(true,StatusCode.OK,"商品删除成功");

    }
}
